const mongoose = require('mongoose');

const categorySchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, '分类名称不能为空'],
    trim: true,
    maxlength: [30, '分类名称最多30个字符']
  },
  description: {
    type: String,
    maxlength: [200, '分类描述最多200个字符']
  },
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: [true, '用户ID不能为空']
  },
  order: {
    type: Number,
    default: 0
  }
}, {
  timestamps: true
});

// 创建索引
categorySchema.index({ userId: 1, name: 1 }, { unique: true });
categorySchema.index({ userId: 1, order: 1 });

const Category = mongoose.model('Category', categorySchema);

module.exports = Category;